package com.xing.dao;

import com.xing.DTO.GeneratorDTO;
import com.xing.entity.TableColumn;
import com.xing.entity.TableListEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


/**
 * @Author Xing
 * @Date 2023/8/8 18:19
 * @version 1.0
 */
@Mapper
public interface TableListDao {
    /**
     * 查询所有表
     */
    @Select("select table_name table_name, table_comment table_comment, create_time create_time from information_schema.tables\n" +
            "\t\t\twhere table_schema = #{tableSchema} and TABLE_NAME not like '%generator' limit ${page},10;")
    public List<TableListEntity> GetTableList(@Param("tableSchema") String tableSchema,
                                              @Param("page") String page);

    /**
     * 查询表数目
     * @return
     */
    @Select("select COUNT(*) from information_schema.tables\n" +
            "\t\t\twhere table_schema = #{tableSchema} and TABLE_NAME != 'miao_generator' ")
    public Long GetTableNumber(@Param("tableSchema") String tableSchema);
    /**
     * 查询表内字段
     * @return
     */
    @Select("select column_name, data_type, column_comment from information_schema.columns \n" +
            "where table_name = #{tableName} and table_schema = #{tableSchema} order by ordinal_position;")
    public List<TableColumn> GetColumns(@Param("tableName") String tableName,
                                        @Param("tableSchema") String tableSchema);

    /**
     * 查询设置
     * @return
     */
    @Select("SELECT * FROM miao_generator WHERE id = '0';")
    public GeneratorDTO getGenerator();

    /**
     * 修改设置
     * @param dto
     * @return
     */
    @Update("UPDATE miao_generator SET url=#{url},model_name=#{modelName}" +
            ",author=#{author},table_prefix=#{tablePrefix},id_type=#{idType} WHERE id='0';")
    public Boolean chang(GeneratorDTO dto);

    /**
     * 获取当前数据库名称
     * @return
     */
    @Select("select database() as name;")
    public String getDatabaseName();
}
